import QtQuick 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0

Item {
    Image {
        id: bg
        source: "qrc:/res/bg.png"
        anchors.fill: parent
    }

    Text {
        id: mainTitle
        font.bold: true
        font.family: "Microsoft Yahei"
        font.pixelSize: 32
        color: "white"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: parent.height / 6
        font.letterSpacing: parent.width / 80
        text: "智能装配演示平台"
    }
    Text {
        id: subTitle
        opacity: 0.7
        font.family: "Microsoft Yahei"
        font.pixelSize: 20
        color: "white"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: mainTitle.bottom
        anchors.topMargin: parent.height / 20
        font.letterSpacing: parent.width / 200
        text: "Intelligent Assembly Demonstration Platform"
    }

    Row {
        anchors.centerIn: parent
        anchors.verticalCenterOffset: bg.height / 10
        spacing: bg.width / 20
        Repeater {
            model: ListModel {
                ListElement { btnText: "装配序列\n路径仿真"; btnSource: "qrc:/res/path.svg" }
                ListElement { btnText: "装配精度\n公差优化"; btnSource: "qrc:/res/data.svg" }
                ListElement { btnText: "VR装配仿真"; btnSource: "qrc:/res/vr.svg" }
                ListElement { btnText: "退出平台"; btnSource: "qrc:/res/tool.svg" }
            }
            delegate: Rectangle {
                radius: height / 10
                height: bg.height / 4
                width: bg.width / 7
                opacity: 0.8
                scale: (pressArea.pressed || pressArea.hover) ? 0.95 : 1
                Image {
                    anchors.top: parent.top
                    anchors.topMargin: parent.height / 10
                    anchors.horizontalCenter: parent.horizontalCenter
                    sourceSize.height: parent.height * 0.4
                    source: btnSource
                }
                Text {
                    id: pressText
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: parent.height / 10
                    anchors.horizontalCenter: parent.horizontalCenter
                    font.family: "Microsoft Yahei"
                    font.pixelSize: 18
                    text: btnText
                    color: "#222222"
                }
                MouseArea {
                    id: pressArea
                    property bool hover: false
                    anchors.fill: parent
                    onEntered: hover = true
                    onExited: hover = false
                    hoverEnabled: true
                    onClicked: {
                        if(index == 3) {
                            messageDialog.text = "是否要退出平台?"
                            messageDialog.open()
                        }
                        else {
                            failedTimer.start()
                            myLoadingView.open()
                            GET_ROOT.startApp(index)
                        }
                    }
                }
            }
        }
    }

    Timer {
        id: failedTimer
        interval: 10000
        repeat: false
        running: false
        onTriggered: {
            if(myLoadingView.visible) {
                myLoadingView.close()
                messageDialog.text = "启动失败!"
                messageDialog.open()
            }
        }
    }

    Popup {
        id: messageDialog
        width: parent.width
        height: parent.height
        visible: false
        property alias text: titleText.text
        background: Rectangle {
            color: Qt.rgba(0, 0, 0, 0.5)
        }
        Rectangle {
            height: 300
            width: 400
            radius: 10
            anchors.centerIn: parent
            Text {
                id: titleText
                anchors.centerIn: parent
                font.family: "Microsoft Yahei"
                font.pixelSize: 16
                color: "#222222"
                text: "是否要退出平台?"
            }

            Row {
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 20
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: 20
                Repeater {
                    model: ["qrc:/res/ok.svg", "qrc:/res/ng.svg"]
                    delegate: Rectangle {
                        height: 50
                        width: 50
                        radius: width / 2
                        color: "#EEEEEE"
                        scale: (boxArea.pressed || boxArea.hover) ? 0.8 : 1
                        Image {
                            source: modelData
                            sourceSize.height: parent.height * 0.8
                            anchors.centerIn: parent
                        }
                        MouseArea {
                            id: boxArea
                            property bool hover: false
                            anchors.fill: parent
                            onEntered: hover = true
                            onExited: hover = false
                            onClicked: {
                                if(index == 0) {
                                    if(titleText.text == "是否要退出平台?") GET_ROOT.quit()
                                    else messageDialog.close()
                                }
                                else messageDialog.close()
                            }
                        }
                    }
                }
            }
        }
    }

    Popup {
        id: myLoadingView
        width: parent.width
        height: parent.height
        visible: false
        property string text: "正在启动..."
        background: Rectangle {
            color: Qt.rgba(0.8, 0.8, 0.8, 0.6)
        }
        Rectangle {
            height: 200
            width: 400
            radius: 10
            anchors.centerIn: parent
            Item {
                id: maskLoading
                height: 50
                width: 50
                anchors.centerIn: parent
                Rectangle {
                    id: rect
                    width: parent.width
                    height: parent.height
                    color: Qt.rgba(0, 0, 0, 0)
                    radius: width / 2
                    border.width: width / 6
                    visible: false
                }

                ConicalGradient {
                    width: rect.width
                    height: rect.height
                    gradient: Gradient {
                        GradientStop{ position: 0.0; color: "blue" }
                        GradientStop{ position: 1.0; color: "gray" }
                    }
                    source: rect
                    Rectangle {
                        anchors.top: parent.top
                        anchors.horizontalCenter: parent.horizontalCenter
                        width: rect.border.width
                        height: width
                        radius: width / 2
                        color: "blue"
                    }

                    RotationAnimation on rotation {
                        from: 0
                        to: 360
                        duration: 1000
                        loops: Animation.Infinite
                    }
                }
            }
            Text {
                anchors.top: maskLoading.bottom
                anchors.topMargin: 20
                anchors.horizontalCenter: parent.horizontalCenter
                font.family: "Microsoft Yahei"
                font.pixelSize: 16
                color: "black"
                text: myLoadingView.text
            }
        }
    }

    Timer {
        id: bankTimer
        interval: 2000
        repeat: false
        running: false
        onTriggered: {
            myLoadingView.close()
        }
    }

    Connections {
        target: GET_ROOT
        onStarted: {
            console.log("FFF")
            if(failedTimer.running) {
                failedTimer.stop()
                bankTimer.start()
            }
        }
    }
}
